﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Inaction.Security
{
    public static class DEncrypt
    {
        #region Hash
        private static byte[] MakeMD5(byte[] original)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            byte[] keyhash = hashmd5.ComputeHash(original);
            hashmd5.Dispose();
            return keyhash;
        }
        public static byte[] MakeMD5(string original)
        {
            var b = System.Text.Encoding.UTF8.GetBytes(original);
            return MakeMD5(b);
        }
        public static byte[] MakeHashSalt(byte[] original)
        {
            SHA256Managed m = new SHA256Managed();
            var key = new List<byte>();

            key.AddRange(original);

            key.Insert(0, original[original.Length - 1]);
            key.Insert(key.Count / 2, original[original.Length / 2]);
            key.Add(original[0]);

            byte[] hask = m.ComputeHash(key.ToArray());
            m.Dispose();
            return hask;
        }
        public static byte[] MakeHashSalt(string original)
        {
            var b = System.Text.Encoding.UTF8.GetBytes(original);
            return MakeHashSalt(b);
        }
        #endregion

        #region DES
        public static string DESEncryptStr(string encryptString, string encryptKey)
        {
            try
            {
                byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                DESCryptoServiceProvider dES = new DESCryptoServiceProvider();
                byte[] byteEncrypt = Encoding.UTF8.GetBytes(encryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, dES.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteEncrypt, 0, byteEncrypt.Length);
                cs.FlushFinalBlock();
                var ret = Convert.ToBase64String(ms.ToArray());
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static byte[] DESEncrypt(string encryptString, string encryptKey)
        {
            try
            {
                byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                DESCryptoServiceProvider dES = new DESCryptoServiceProvider();
                byte[] byteEncrypt = Encoding.UTF8.GetBytes(encryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, dES.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteEncrypt, 0, byteEncrypt.Length);
                cs.FlushFinalBlock();
                var ret = ms.ToArray();
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string DESDecryptStr(string decryptString, string decryptKey)
        {
            try
            {
                byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                DESCryptoServiceProvider dES = new DESCryptoServiceProvider();
                byte[] byteDecryptString = Convert.FromBase64String(decryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, dES.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteDecryptString, 0, byteDecryptString.Length);
                cs.FlushFinalBlock();
                var ret = Encoding.UTF8.GetString(ms.ToArray());
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static byte[] DESDecrypt(string decryptString, string decryptKey)
        {
            try
            {
                byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                DESCryptoServiceProvider dES = new DESCryptoServiceProvider();
                byte[] byteDecryptString = Convert.FromBase64String(decryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, dES.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteDecryptString, 0, byteDecryptString.Length);
                cs.FlushFinalBlock();
                var ret = ms.ToArray();
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string DES3Encrypt(string encryptString, string encryptKey1, string encryptKey2, string encryptKey3)
        {
            try
            {
                var returnValue = DESEncryptStr(encryptString, encryptKey3);
                returnValue = DESEncryptStr(returnValue, encryptKey2);
                return DESEncryptStr(returnValue, encryptKey1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static string DES3Decrypt(string decryptString, string decryptKey1, string decryptKey2, string decryptKey3)
        {
            try
            {
                var returnValue = DESDecryptStr(decryptString, decryptKey1);
                returnValue = DESDecryptStr(returnValue, decryptKey2);
                return DESDecryptStr(returnValue, decryptKey3);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region RC2
        public static string RC2EncryptStr(string encryptString, string encryptKey)
        {
            try
            {
                byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                RC2CryptoServiceProvider rC2 = new RC2CryptoServiceProvider();
                byte[] byteEncryptString = Encoding.UTF8.GetBytes(encryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, rC2.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteEncryptString, 0, byteEncryptString.Length);
                cs.FlushFinalBlock();
                var ret = Convert.ToBase64String(ms.ToArray());
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static byte[] RC2Encrypt(string encryptString, string encryptKey)
        {
            try
            {
                byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                RC2CryptoServiceProvider rC2 = new RC2CryptoServiceProvider();
                byte[] byteEncryptString = Encoding.UTF8.GetBytes(encryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, rC2.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteEncryptString, 0, byteEncryptString.Length);
                cs.FlushFinalBlock();
                var ret = ms.ToArray();
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string RC2DecryptStr(string decryptString, string decryptKey)
        {
            try
            {
                byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                RC2CryptoServiceProvider rC2 = new RC2CryptoServiceProvider();
                byte[] byteDecrytString = Convert.FromBase64String(decryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, rC2.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteDecrytString, 0, byteDecrytString.Length);
                cs.FlushFinalBlock();
                var ret = Encoding.UTF8.GetString(ms.ToArray());
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static byte[] RC2Decrypt(string decryptString, string decryptKey)
        {
            try
            {
                byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                RC2CryptoServiceProvider rC2 = new RC2CryptoServiceProvider();
                byte[] byteDecrytString = Convert.FromBase64String(decryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, rC2.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteDecrytString, 0, byteDecrytString.Length);
                cs.FlushFinalBlock();
                var ret = ms.ToArray();
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region AES
        public static string AESEncryptStr(string encryptString, string encryptKey)
        {
            byte[] temp = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
            Rijndael AESProvider = Rijndael.Create();
            try
            {
                byte[] byteEncryptString = Encoding.UTF8.GetBytes(encryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, AESProvider.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteEncryptString, 0, byteEncryptString.Length);
                cs.FlushFinalBlock();
                var ret = Convert.ToBase64String(ms.ToArray());
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static byte[] AESEncrypt(string encryptString, string encryptKey)
        {
            byte[] temp = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
            Rijndael AESProvider = Rijndael.Create();
            try
            {
                byte[] byteEncryptString = Encoding.UTF8.GetBytes(encryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, AESProvider.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteEncryptString, 0, byteEncryptString.Length);
                cs.FlushFinalBlock();
                var ret = ms.ToArray();
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string AESDecryptStr(string decryptString, string decryptKey)
        {
            byte[] temp = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
            Rijndael AESProvider = Rijndael.Create();
            try
            {
                byte[] byteDecryptString = Convert.FromBase64String(decryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, AESProvider.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteDecryptString, 0, byteDecryptString.Length);
                cs.FlushFinalBlock();
                var ret = Encoding.UTF8.GetString(ms.ToArray());
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static byte[] AESDecrypt(string decryptString, string decryptKey)
        {
            byte[] temp = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
            Rijndael AESProvider = Rijndael.Create();
            try
            {
                byte[] byteDecryptString = Convert.FromBase64String(decryptString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, AESProvider.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), temp), CryptoStreamMode.Write);
                cs.Write(byteDecryptString, 0, byteDecryptString.Length);
                cs.FlushFinalBlock();
                var ret = ms.ToArray();
                cs.Close();
                ms.Close();
                cs.Dispose();
                ms.Dispose();
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
    }
}
